package com.jcli._java_swing_入门教程pdf.ch17;

import javax.swing.*;
import java.awt.*;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;

/**
 * @author li
 * @version 1.0
 * @belongsProject novel_20230923
 * @belongsPackage com.jcli._java_swing_入门教程pdf.ch17
 * @className CardLayoutDemo
 * @description TODO  卡片布局管理器
 * @createTime 2024-01-15  11:37
 */
public class CardLayoutDemo {
    public static void main(String[] args) {
        JFrame jFrame = new JFrame("Java第五个GUI程序");
        JPanel panel1 = new JPanel();
        JPanel panel2 = new JPanel();
        JPanel cards = new JPanel(new CardLayout());
        panel1.add(new JButton("登录按钮"));
        panel1.add(new JButton("注册按钮"));
        panel1.add(new JButton("找回密码按钮"));
        panel2.add(new JTextField("用户文本框", 20));
        panel2.add(new JTextField("密码文本框", 20));
        panel2.add(new JTextField("验证码文本框", 20));
        //向卡片式布局中添加面板
        cards.add(panel1, "cards1");
        cards.add(panel2, "cards2");
        CardLayout c1 = (CardLayout) cards.getLayout();

        // c1.show(cards,"cards2");
        JButton jbb = new JButton("按钮");
        JButton jbt = new JButton("文本框");
        jbb.addActionListener(new ActionListener() {
            @Override
            public void actionPerformed(ActionEvent e) {
                c1.show(cards, "cards1");
            }
        });
        jbt.addActionListener(new ActionListener() {
            @Override
            public void actionPerformed(ActionEvent e) {
                c1.show(cards, "cards2");
            }
        });
        JPanel jp4 = new JPanel();
        jp4.add(jbb);
        jp4.add(jbt);
        //此处这个布局不是最佳的
        jFrame.add(jp4,BorderLayout.NORTH);
        jFrame.add(cards,BorderLayout.SOUTH);
        jFrame.setBounds(300, 400, 400, 400);
        jFrame.setVisible(true);
        jFrame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
    }
}
